/**
 * 从商品 SKU 数组中，转换出商品属性的数组
 *
 * 类似结构：[{
 *    id: // 属性的编号
 *    name: // 属性的名字
 *    values: [{
 *      id: // 属性值的编号
 *      name: // 属性值的名字
 *    }]
 * }]
 *
 */
import { fenToYuan } from '@/utils'

const getPropertyList = (skus: any): EmptyArrayType[] => {
  const result = <EmptyArrayType>[]
  for (const sku of skus) {
    if (!sku.properties) {
      continue
    }
    for (const property of sku.properties) {
      // ① 先处理属性
      let resultProperty = <any>result.find((item: any) => item.id === property.propertyId)
      if (!resultProperty) {
        resultProperty = {
          id: property.propertyId,
          name: property.propertyName,
          values: []
        }
        result.push(resultProperty)
      }
      // ② 再处理属性值
      const resultValue = resultProperty.values.find((item) => item.id === property.valueId)
      if (!resultValue) {
        resultProperty.values.push({
          id: property.valueId,
          name: property.valueName
        })
      }
    }
  }
  return result
}
const appendSettlementProduct = (spus, settlementInfos) => {
  if (!settlementInfos || settlementInfos.length === 0) {
    return
  }
  for (const spu of spus) {
    const settlementInfo = settlementInfos.find((info) => info.spuId === spu.spuId)
    // 设置购买权限
    spu.buyEnable = settlementInfo.buyEnable
    if (!settlementInfo) {
      return
    }

    // 选择价格最小的 SKU 设置到 SPU 上
    // const settlementSku = settlementInfo.skus
    //   .filter((sku) => sku.promotionPrice > 0)
    //   .reduce((prev, curr) => (prev.promotionPrice < curr.promotionPrice ? prev : curr))
    // if (settlementSku) {
    //   spu.promotionType = settlementSku.promotionType
    //   spu.promotionPrice = settlementSku.promotionPrice
    // }
    // // 设置【满减送】活动
    if (settlementInfo.rewardActivity) {
      spu.rewardActivity = settlementInfo.rewardActivity
    }
  }
}
/**
 * 将分转成元
 *
 * 如果没有小数点，则不展示小数点部分
 *
 * @param price 分，例如说 100 分
 * @returns {string} 元，例如说 1 元
 */
export function fen2yuanSimple(price) {
  return fenToYuan(price).replace(/\.?0+$/, '')
}
// 获得满减送活动的规则描述（item）
const getRewardActivityRuleItemDescriptions = (activity) => {
  if (!activity || !activity.rules || activity.rules.length === 0) {
    return []
  }
  const result = [] as EmptyArrayType
  activity.rules.forEach((rule) => {
    const conditionTypeStr =
      activity.conditionType === 10 ? `满${fen2yuanSimple(rule.limit)}元` : `满${rule.limit}件`
    // 满减
    if (rule.limit) {
      result.push(`${conditionTypeStr}减${fen2yuanSimple(rule.discountPrice)}元`)
    }
    // 赠品
    if (rule.point) {
      result.push(`${conditionTypeStr}送${rule.point}积分`)
    }
    if (rule.giveCouponTemplateCounts && rule.giveCouponTemplateCounts.length > 0) {
      result.push(`${conditionTypeStr}送${rule.giveCouponTemplateCounts.length}张优惠券`)
    }
    // 包邮
    if (rule.freeDelivery) {
      result.push(`${conditionTypeStr}包邮`)
    }
  })
  return result
}
export { getPropertyList, appendSettlementProduct, getRewardActivityRuleItemDescriptions }
